package com.stork.cms.fragment;

import android.app.AlertDialog;
import android.app.Fragment;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.ServiceHandle.Soap.ServiceCall;
import com.stork.cms.R;
import com.stork.cms.activity.GridHomeMenuActivity;
import com.stork.cms.activity.changepassword;

public class LoginPageFragment extends Fragment implements OnClickListener{

	Button LoginButton;
	EditText UserName,Password;
	String response=null;
	ProgressDialog progreess;
	String sendusername=null, sendpassword=null;
	TextView errortext;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View login=inflater.inflate(R.layout.login, null);
		LoginButton=(Button)login.findViewById(R.id.loginbutton1);
		UserName=(EditText)login.findViewById(R.id.username);
		Password=(EditText)login.findViewById(R.id.password);
		errortext=(TextView)login.findViewById(R.id.seterrortext);
		LoginButton.setOnClickListener(this);
		
		return login;		
	}	

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		
		 sendusername=UserName.getText().toString();
		 sendpassword=Password.getText().toString();
		if(sendusername.equals(""))
		{
			UserName.setError("* Enter the Username");
			UserName.requestFocus();
		}
		else if(sendpassword.equals(""))
		{
			Password.setError("* Enter password ");
			Password.requestFocus();
		}
		else
		{
		new GetResPonse().execute();
		/*Intent i=new Intent(MainActivity.this,Attendencedate.class);
		startActivity(i);*/
		}	
		
}
	public class GetResPonse extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			
			progreess=new ProgressDialog(getActivity());
			progreess.setMessage("Please Wait...");
			progreess.setCancelable(false);
			progreess.show();
		}
		@Override
		protected void onPostExecute(Void result) {
			// TODO Auto-generated method stub
			super.onPostExecute(result);
			if(progreess.isShowing())
			{
				progreess.dismiss();
			}
		}
		
		@Override
		protected Void doInBackground(Void... Params) {
			// TODO Auto-generated method stub
			
			ServiceCall Sc=new ServiceCall();
			
			
//				passwordtext.setError("Enter the password");
			//String getresponse=Sc.Login(sendusername, sendpassword);
			response=Sc.Studendloginid(sendusername, sendpassword);
			
			Log.d("result", response);
		
			if(response.equals("111"))
			{	mes();
				Intent i=new Intent(getActivity(),GridHomeMenuActivity.class);
				startActivity(i);
				
			}
			else if(response.equals("444"))
			{
				getActivity().runOnUiThread(new Runnable(){
					
					@Override
					public void run() {
				//Log.i("welcome to cms","NO DATA found");
				Toast.makeText(getActivity(), "Invalid Valid User ", Toast.LENGTH_SHORT).show();
				UserName.setText("");
				Password.setText("");
				UserName.requestFocus();
				errortext.setTextColor(Color.RED);
				errortext.setVisibility(View.VISIBLE);
					}
					});
				
			}
			else if(response.equals("222"))
			{
				getActivity().runOnUiThread(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						Toast.makeText(getActivity(), "Do u Want change the password ", Toast.LENGTH_SHORT).show();
						AlertDialog.Builder builder1 = new AlertDialog.Builder(getActivity());
			            builder1.setMessage("Do U Want change the Password");
			            builder1.setCancelable(true);
			            builder1.setPositiveButton("Yes",
			                    new DialogInterface.OnClickListener() {
			                public void onClick(DialogInterface dialog, int id) {
			                    Intent changepasswordintent=new Intent(getActivity(),changepassword.class);
			                    startActivity(changepasswordintent);	                }
			            });
			            builder1.setNegativeButton("No",
			                    new DialogInterface.OnClickListener() {
			                public void onClick(DialogInterface dialog, int id) {
			                    dialog.cancel();
			                }
			            });

			            AlertDialog alert11 = builder1.create();
			            alert11.show();	
					}
				});
			}
			
			
			
			
			
			
			
			return null;
		
		}

		
	private void mes()
	 {	
		
		 	        Intent intent = new Intent(getActivity(),LoginPageFragment.class);
		 	        PendingIntent pIntent = PendingIntent.getActivity(getActivity(), 0, intent, 0);
		 	 
		 	   
		 	        NotificationCompat.Builder mb=new NotificationCompat.Builder(getActivity());
		 	           mb .setContentTitle("Stork System");
		 	            mb.setContentText("Welcome to Stork Systems Pvt.ltd");
		 	            mb.setSmallIcon(R.drawable.notificationlogo);
		 	            mb.setContentIntent(pIntent);
		 	            mb.addAction(R.drawable.notificationlogo, "View", pIntent);
		 	            mb.addAction(0, "Remind", pIntent);
		 	            mb.build();
		 NotificationManager nm=(NotificationManager)getActivity().getSystemService(Context.NOTIFICATION_SERVICE);
		 nm.notify(0,mb.build());
	 }
	}
}
